package com.recharge.controller;

import com.alibaba.fastjson.JSON;
import com.recharge.base.ResponseInfo;
import com.recharge.base.ResponseInfoEnum;
import com.recharge.wk.domain.ProductSup;
import com.recharge.wk.mapper.IProductSupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by qi.cao on 2017/1/24.
 */
@Controller
@RequestMapping("/productSup")
public class ProductSupController {

    @Autowired
    private IProductSupMapper iProductSupMapper;

    @RequestMapping("/queryList")
    @ResponseBody
    public ResponseInfo queryList(
            @RequestParam("productId") String productId,
            @RequestParam("productName") String productName,
            @RequestParam("supId") String supId,
            @RequestParam("supName") String supName,
            @RequestParam("status") Boolean status,
            @RequestParam("curr") Integer curr,
            @RequestParam("pageSize") Integer pageSize,
            HttpServletRequest request) {
        ResponseInfo responseInfo = new ResponseInfo();
        ProductSup productSup = new ProductSup();
        productSup.setProductName(productName);
        productSup.setProductId(productId);
        productSup.setSupId(supId);
        productSup.setSupName(supName);
        productSup.setStatus(status);

        List<ProductSup> productSups = iProductSupMapper.selectList(productSup,curr,pageSize);

        responseInfo.setCount(iProductSupMapper.count(productSup));
        responseInfo.setData(productSups);
        return responseInfo;
    }


    @RequestMapping("/updateInfo")
    @ResponseBody
    public ResponseInfo updateInfo(
            @RequestParam("productId") String productId,
            @RequestParam("supId") String supId,
            @RequestParam("priority") BigDecimal priority,
            @RequestParam("weight") BigDecimal weight,
            @RequestParam(value = "status" , required = false) Boolean status) {
        ResponseInfo responseInfo = new ResponseInfo();

        ProductSup productSup = new ProductSup();
        productSup.setProductId(productId);
        productSup.setSupId(supId);
        productSup.setPriority(priority);
        productSup.setStatus(status);
        productSup.setWeight(weight);

        iProductSupMapper.update(productSup);
        responseInfo.setInfoEnum(ResponseInfoEnum.SUCCESS);
        return responseInfo;
    }


    @RequestMapping("/addInfo")
    @ResponseBody
    public ResponseInfo addInfo(@RequestParam("context")String context) {
        ResponseInfo responseInfo = new ResponseInfo();

        List<ProductSup> productSups = JSON.parseArray(context, ProductSup.class);

        for (ProductSup productSup : productSups){
            try {
                iProductSupMapper.insert(productSup);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        responseInfo.setInfoEnum(ResponseInfoEnum.SUCCESS);
        return responseInfo;
    }
}
